package com.geotmt.billingcenter.modules.consume.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.geotmt.billingcenter.common.entry.ConsumeBaseEntry;
import com.geotmt.billingcenter.common.entry.StairRule;
import com.geotmt.billingcenter.common.enumpackage.ConsumeFeeFlagEnum;
import com.geotmt.billingcenter.common.enumpackage.RuleLabelEnum;
import com.geotmt.billingcenter.common.vo.consume.ConsumeHisVo;
import com.geotmt.billingcenter.common.vo.consume.ConsumeVo;
import com.geotmt.billingcenter.common.vo.params.ParamsConsume;
import com.geotmt.billingcenter.common.vo.params.ParamsForDel;
import com.geotmt.billingcenter.modules.consume.mapper.ConsumeBaseMapper;
import com.geotmt.billingcenter.modules.consume.service.ConsumeBaseService;
import com.geotmt.billingcenter.modules.rules.service.RulesService;
import com.github.pagehelper.PageHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Description 类描述
 * @Author      yanghanwei
 * @Mail        yanghanwei@geotmt.com
 * @Date        2019/3/13 10:52
 * @Version     v1
 **/
@Service
@Transactional(rollbackFor = Exception.class)
public class ConsumeBaseServiceImpl implements ConsumeBaseService {

    private static final Logger logger = LoggerFactory.getLogger(ConsumeBaseServiceImpl.class);

    @Resource
    private ConsumeBaseMapper consumeBaseMapper;

    @Resource
    private RulesService rulesService;

    /**
     * 添加base的同时添加rule
     * @param consumeBase
     * @return
     */
    @Override
    public Integer addBase(ConsumeBaseEntry consumeBase) {
        logger.info("消耗 subBase add service");
        consumeBaseMapper.addBase(consumeBase);
        return consumeBase.getId();
    }

    /**
     * 修改base的同时添加rule
     * @param consumeVo
     * @return
     */
    @Override
    public int updateBase(ConsumeVo consumeVo) {
        logger.info("消耗 subBase update service");
        return consumeBaseMapper.updateBase(consumeVo);
    }

    /**
     * 删除base
     * @param obj
     * @return
     */
    @Override
    public int deleteBase(ParamsForDel obj) {
        logger.info("消耗 subBase delete service");
        return consumeBaseMapper.deleteBase(obj);
    }

    @Override
    public List<ConsumeVo> listCurr(ParamsConsume obj) {
        Integer pageNum = obj.getPageNum();
        Integer pageSize = obj.getPageSize();
        String orderBy = obj.getOrderBy();
        if(null != pageNum && null != pageSize){
            PageHelper.startPage(pageNum,pageSize);
        }
        if(!StringUtils.isEmpty(orderBy)){
            PageHelper.orderBy(orderBy);
        }
        List<ConsumeVo> consumeVos = consumeBaseMapper.listCurr(obj);
        //查询 规则的详细计费规则
        if(null != consumeVos){
            for(ConsumeVo consumeVo : consumeVos){
                if(ConsumeFeeFlagEnum.STAIR.value.equals(consumeVo.getFeeFlag())){
                    List<StairRule> stairRules = rulesService.stairListGetByRid(consumeVo.getRId(), RuleLabelEnum.CONSUME_CURR.value);
                    consumeVo.setStairRuleList(stairRules);
                }
            }
        }
        return consumeVos;
    }

    @Override
    public List<ConsumeVo> listAll(ParamsConsume obj) {
        String orderBy = obj.getOrderBy();
        if(!StringUtils.isEmpty(orderBy)){
            PageHelper.orderBy(orderBy);
        }
        List<ConsumeVo> consumeVos = consumeBaseMapper.listAll(obj);
        //查询 规则的详细计费规则
        if(null != consumeVos){
            for(ConsumeVo consumeVo : consumeVos){
                if(ConsumeFeeFlagEnum.STAIR.value.equals(consumeVo.getFeeFlag())){
                    consumeVo.setStairRuleList(rulesService.stairListGetByRid(consumeVo.getRId(), RuleLabelEnum.CONSUME_CURR.value));
                }
            }
        }
        return consumeVos;
    }

    @Override
    public ConsumeVo currGetById(Integer id) {
        ConsumeVo consume = consumeBaseMapper.getById(id);
        //查询 规则的详细计费规则
        if(ConsumeFeeFlagEnum.STAIR.value.equals(consume.getFeeFlag())){
            consume.setStairRuleList(rulesService.stairListGetByRid(consume.getRId(), RuleLabelEnum.CONSUME_HIS.value));
        }
        return consume;
    }

    @Override
    public ConsumeHisVo getConsume(String code, Integer companyId, Integer cuserId, Integer interfaceId, String date) {
        logger.info("获取当前有效的规则 base service");
        ConsumeHisVo consume = consumeBaseMapper.getConsume(code, companyId, cuserId, interfaceId, date);
        //查询 规则的详细计费规则
        if(null == consume){
            return null;
        }
        //sign 是在上班查询的时候 sql 拼接进去的
        if(ConsumeFeeFlagEnum.STAIR.value.equals(consume.getFeeFlag())){
            Integer ruleLabel = null;
            if("curr".equals(consume.getSign())){
                ruleLabel = RuleLabelEnum.CONSUME_CURR.value;
            }else if("his".equals(consume.getSign())){
                ruleLabel = RuleLabelEnum.CONSUME_HIS.value;
            }
            consume.setStairRuleList(rulesService.stairListGetByRid(consume.getRId(), ruleLabel));
        }
        return consume;
    }
}
